package client.comm.baoding.adapter

import android.content.Context
import android.content.Intent
import android.text.TextUtils
import android.util.TypedValue
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import client.comm.commlib.base.BaseLoadmoreAdapter
import client.comm.commlib.base.BindingViewHoder
import client.comm.commlib.toast.toast
import client.comm.baoding.R
import client.comm.baoding.api.bean.GoodsOrder
import client.comm.baoding.databinding.LayoutGoodswrapitemBinding
import client.comm.baoding.databinding.LayoutOrderitemBinding
import client.comm.baoding.ui.ApplyTuiKuanActivity
import client.comm.baoding.ui.GoodsOrderDetailActivity
import client.comm.baoding.ui.WlDetailActivity
import com.bumptech.glide.RequestManager

class OrderAdapter : BaseLoadmoreAdapter<GoodsOrder.Order> {

    val manager: RequestManager

    constructor(context: Context, manager: RequestManager) : super(context) {
        this.manager = manager
    }

    override fun createVh(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.layout_orderitem,
                parent,
                false
            )
        )
    }

    override fun bindVh(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = bindingViewHolder.binding as LayoutOrderitemBinding
        val bean = getItem(position)
        binding.bean = bean
        binding.isShowTuiKuan = false
        when (bean.status) {
            0 -> {
                binding.status = "待付款"
            }
            1 -> {
                binding.status = "待发货"
                binding.isShowTuiKuan = true
            }
            2 -> {
                binding.status = "待收货"
                binding.isShowTuiKuan = true
            }
            3 -> {
                binding.status = "已完成"
            }
            4 -> {
                binding.status = "失败"
            }
        }

        binding.commPriceIntegral.apply {
            comm_price_integral(
                tvPriceUnit,
                tvPrice,
                bean.actual_price,
                tvIntegral,
                bean.integral,
                tvIntegralDesc
            ) //bean.lpq_price,
        }


        binding.itemWrap.removeAllViews()
        for (data in bean.goods_list) {
            DataBindingUtil.inflate<LayoutGoodswrapitemBinding>(
                LayoutInflater.from(context), R.layout.layout_goodswrapitem, binding.itemWrap, false
            ).apply {
                manager.load(data.goods_img).error(R.mipmap.ic_launcher).into(img)
                this.bean = data

                commPriceIntegral.apply {
                    comm_price_integral(
                        tvPriceUnit,
                        tvPrice,
                        data.price,
//                        data.lpq_price,
                        tvIntegral,
                        data.integral,
                        tvIntegralDesc
                    )
                }

//                if (i.price > 0) {
//                    tvPrice.text = "¥${i.price}"
//                    if (i.integral > 0) {
//                        tvIntegral.text = "+" + i.integral + "积分"
//                    } else {
//                        tvIntegral.text = ""
//                    }
//
//                } else {
//                    tvPrice.text = ""
//                    if (i.integral > 0) {
//                        tvIntegral.text  = "" + i.integral + "积分"
//                    } else {
//                        tvIntegral.text = ""
//                    }
//
//                }

                binding.itemWrap.addView(root)
            }
        }
        binding.event = this
    }

    /**
     * 通用商品价格和积分格式化
     */
    fun comm_price_integral(
        tvPriceUnit: TextView,
        tvPrice: TextView,
        price: Double,
//        lpq_price: Double,
        tvIntegral: TextView,
        integral: Double,
        tvIntegralDesc: TextView
    ) {
        var is_price_show: Boolean = false
        var is_integral_unit_show: Boolean = false

        if (price > 0) {
            tvPrice.text = "${price}"
            is_price_show = true
            if (integral > 0) {
                tvIntegral.text = "+${integral}" //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }

        }/* else if (lpq_price > 0) {
            TextTool.getBuilder("${lpq_price}")
                .setAlign(Layout.Alignment.ALIGN_CENTER).setTextSize(18)
                .append("体验券").setForegroundColor(Color.parseColor("#000000"))
                .setTextSize(12)
                .into(tvPrice)
            is_price_show = true
            if (integral > 0) {
                tvIntegral.text = "+" + integral //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }

        } */else {
            tvPrice.text = ""
            is_price_show = false
            if (integral > 0) {
                tvIntegral.text = "" + integral //+ "积分"
                is_integral_unit_show = true
            } else {
                tvIntegral.text = ""
                is_integral_unit_show = false
            }
        }

        if (is_price_show) {
            tvPriceUnit.visibility = View.VISIBLE
        } else {
            tvPriceUnit.visibility = View.GONE
        }

        if (is_integral_unit_show) {
            tvIntegralDesc.visibility = View.VISIBLE
        } else {
            tvIntegralDesc.visibility = View.GONE
        }
        tvPrice.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)
        tvIntegral.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16F)

        tvPriceUnit.setTextColor(context.resources!!.getColor(client.comm.commlib.R.color.txt_color))
        tvPrice.setTextColor(context.resources!!.getColor(client.comm.commlib.R.color.txt_color))
        tvIntegral.setTextColor(context.resources!!.getColor(client.comm.commlib.R.color.txt_color))
        tvIntegralDesc.setTextColor(context.resources!!.getColor(client.comm.commlib.R.color.txt_color))

    }

    fun itemClick(bean: GoodsOrder.Order) {
        context.startActivity(Intent(context, GoodsOrderDetailActivity::class.java).apply {
            putExtra("bean", bean)
        })
    }


    fun goTUiKuan(bean: GoodsOrder.Order) {
        context.startActivity(Intent(context, ApplyTuiKuanActivity::class.java).apply {
            putExtra("bean", bean)
        })
    }

    var orderPay: ((bean: GoodsOrder.Order) -> Unit)? = null

    fun goPay(bean: GoodsOrder.Order) {
        orderPay?.invoke(bean)
    }

    var cancle: ((bean: GoodsOrder.Order) -> Unit)? = null

    fun cancleOrder(bean: GoodsOrder.Order) {
        cancle?.invoke(bean)
    }

    var qrsh: ((bean: GoodsOrder.Order) -> Unit)? = null

    fun qrshOrder(bean: GoodsOrder.Order) {
        qrsh?.invoke(bean)
    }

    fun lookWl(b: GoodsOrder.Order) {
        if (TextUtils.isEmpty(b.logistics_name) || TextUtils.isEmpty(b.logistics_num)) {
            toast("暂无物流信息")
            return
        }
        context.startActivity(Intent(context, WlDetailActivity::class.java).apply {
            putExtra("orderId", b.order_id)
        })
//        LookWlDialog(context).apply {
//            bean = b
//        }.show()

    }
}